package com.yjc.hire.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.hire.auth.mapper.UserMapper;
import com.yjc.hire.auth.pojo.Users;
import com.yjc.hire.auth.service.UserService;
import com.yjc.hire.auth.utils.DesensitizationUtil;
import com.yjc.hire.enums.Sex;
import com.yjc.hire.enums.ShowWhichName;
import com.yjc.hire.enums.UserRole;
import com.yjc.hire.exceptions.GraceException;
import com.yjc.hire.feign.WorkMicroServiceFeign;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.utils.LocalDateUtils;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 *
 * @author jcyin
 * @since 2025/6/29
 * </p>
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,Users> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Resource
    private WorkMicroServiceFeign workMicroServiceFeign;

    @Override
    public Users queryMobileIsExist(String mobile) {
        return userMapper.selectByMobile(mobile);
    }

    @Override
//    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public Users createUser(String mobile) {
        Users user = new Users();
        user.setMobile(mobile);
        user.setNickname("用户" + DesensitizationUtil.commonDisplay(mobile));
        user.setRealName("用户" + DesensitizationUtil.commonDisplay(mobile));
        user.setShowWhichName(ShowWhichName.nickname.type);
        user.setSex(Sex.secret.type);
        user.setFace("");
        user.setEmail("");
        LocalDate birthday = LocalDateUtils.parseLocalDate("1980-01-01", LocalDateUtils.DATE_PATTERN);
        user.setCountry("中国");
        user.setDistrict("");
        user.setDescription("这家伙很神秘什么都没有留下");
        user.setProvince("");
        user.setCity("");
        user.setStartWorkDate(LocalDate.now());
        user.setBirthday(birthday);

        user.setPosition("底层码农");
        user.setRole(UserRole.CANDIDATE.type);
        user.setHrInWhichCompanyId("");
        user.setCreatedTime(LocalDateTime.now());
        user.setUpdatedTime(LocalDateTime.now());
        userMapper.insert(user);

        GraceJSONResult result = workMicroServiceFeign.init(user.getId());
        if (result.getStatus() != 200) {
            String xid = RootContext.getXID();
            if (StringUtils.isNoneBlank(xid)){
                try {
                    GlobalTransactionContext.reload(xid).rollback();
                } catch (TransactionException e) {
                    e.printStackTrace();
                }
                finally {
                    GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);
                }
            }
        }
        return user;
    }
}
